/*
 * To change this template, choose com.googlecode.jackcardgame.tools | Templates
 * and open the template in the editor.
 */

/*
 * ChooseDeck.java
 *
 * Created on 12 Αυγ 2010, 2:28:12 μμ
 */
package com.googlecode.jackcardgame.tools;

import com.googlecode.jackcardgame.cardgame.Main;
import com.googlecode.jackcardgame.cardgame.cards.Card;
import com.googlecode.jackcardgame.cardgame.cards.Deck;
import java.awt.Cursor;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;

/**
 *
 * @author ssoldatos
 */
public class ChooseDeck extends javax.swing.JDialog {

  private static final long serialVersionUID = 5346456456L;
  public String selectedDeck = Main.deckName;
  private int totalDecks = 0;

  /** Creates new form ChooseDeck */
  public ChooseDeck(java.awt.Frame parent, boolean modal) {
    super(parent, modal);
    initComponents();
    createDecks();
    setSize(600,60+ (int) ((Math.ceil(totalDecks / 7) + 1 )* 115));
    setLocationRelativeTo(null);
    setVisible(true);
  }

  /** This method is called from within the constructor to
   * initialize the form.
   * WARNING: Do NOT modify this code. The content of this method is
   * always regenerated by the Form Editor.
   */
  @SuppressWarnings("unchecked")
  // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
  private void initComponents() {

    panel = new com.googlecode.jackcardgame.tools.JTiledPanel(Main.tsoxatile);
    jLabel1 = new javax.swing.JLabel();
    decks = new javax.swing.JPanel();

    setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
    setResizable(false);
    setUndecorated(true);

    panel.setBorder(new javax.swing.border.LineBorder(new java.awt.Color(255, 255, 255), 5, true));

    jLabel1.setFont(jLabel1.getFont().deriveFont(jLabel1.getFont().getStyle() | java.awt.Font.BOLD, jLabel1.getFont().getSize()+5));
    jLabel1.setForeground(new java.awt.Color(255, 255, 255));
    jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
    jLabel1.setText("Choose Deck");

    decks.setOpaque(false);
    decks.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.LEFT));

    javax.swing.GroupLayout panelLayout = new javax.swing.GroupLayout(panel);
    panel.setLayout(panelLayout);
    panelLayout.setHorizontalGroup(
      panelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(panelLayout.createSequentialGroup()
        .addContainerGap()
        .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, 568, Short.MAX_VALUE)
        .addContainerGap())
      .addGroup(panelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(panelLayout.createSequentialGroup()
          .addGap(11, 11, 11)
          .addComponent(decks, javax.swing.GroupLayout.PREFERRED_SIZE, 567, javax.swing.GroupLayout.PREFERRED_SIZE)
          .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
    );
    panelLayout.setVerticalGroup(
      panelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(panelLayout.createSequentialGroup()
        .addContainerGap()
        .addComponent(jLabel1)
        .addContainerGap(149, Short.MAX_VALUE))
      .addGroup(panelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(panelLayout.createSequentialGroup()
          .addGap(37, 37, 37)
          .addComponent(decks, javax.swing.GroupLayout.DEFAULT_SIZE, 132, Short.MAX_VALUE)
          .addContainerGap()))
    );

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addComponent(panel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
    );
    layout.setVerticalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addComponent(panel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
    );

    pack();
  }// </editor-fold>//GEN-END:initComponents
  // Variables declaration - do not modify//GEN-BEGIN:variables
  private javax.swing.JPanel decks;
  private javax.swing.JLabel jLabel1;
  private com.googlecode.jackcardgame.tools.JTiledPanel panel;
  // End of variables declaration//GEN-END:variables

  private void createDecks() {
    File dir = new File(Deck.DECK_PATH);
    File[] deckDirectory = dir.listFiles();
    for (int i = 0; i < deckDirectory.length; i++) {
      File deck = deckDirectory[i];
      if (deck.isDirectory()) {
        String deckname = deck.getName();
        Main.deckName = deckname;
        DeckCard c = new DeckCard(deckname);
        decks.add(c);
        totalDecks++;
      }

    }
    decks.validate();
    decks.repaint();
  }

  class DeckCard extends Card {

    String deckname;
    

    public DeckCard(String deckname) {
      this.deckname = deckname;
      rank = KING;
      suit = HEARTS;
      setShown(true);
      icon.removeMouseListener(icon.getMouseListeners()[0]);
      setCursor(new Cursor(Cursor.HAND_CURSOR));
      icon.addMouseListener(new MouseAdapter() {

        @Override
        public void mouseReleased(MouseEvent e) {
          Main.deckName = DeckCard.this.deckname;
          ChooseDeck.this.dispose();
        }
      });
    }
  }
}
